package me.pplay.playtalk.activity.friends;

import java.util.List;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.adapter.FriendReqyestAdapter;
import me.pplay.playtalk.bean.Chat;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.util.BuddyInfoUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class FriendRquestResultActivity extends BaseActivity{
	private ImageButton btn_back;
	private ListView listView;
	private TextView requestCount;
	private LinearLayout ignoreLayout, acceptLayout;
	
	private final int SETADAPTER = 1;
	private FriendReqyestAdapter adapter;
	private List<Chat> friendList;
	private TpChat tpChat;
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case SETADAPTER:
				if(adapter == null){
					adapter = new FriendReqyestAdapter(FriendRquestResultActivity.this, friendList, handler);
					listView.setAdapter(adapter);
				}else{
					adapter.notifyDataSetChanged();
				}
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_friend_request);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		findViews();
		initDatas();
		clickEvents();
	}
	
	private void findViews(){
		btn_back		= (ImageButton)findViewById(R.id.btn_back);
		listView		= (ListView)findViewById(R.id.listview);	
		requestCount	= (TextView)findViewById(R.id.requestCount);
		ignoreLayout	= (LinearLayout)findViewById(R.id.ignoreLayout);
		acceptLayout	= (LinearLayout)findViewById(R.id.acceptLayout);
	}
	
	private void initDatas(){
		tpChat			= (new ApplicationTrans()).getTpChat();
		Bundle bundle 	= getIntent().getExtras();
		friendList		= (List<Chat>)bundle.get("friendlist"); 
		requestCount.setText(friendList.size());
		handler.sendEmptyMessage(SETADAPTER);
	}
	
	private void clickEvents(){
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FriendRquestResultActivity.this.finish();
			}
		});
		
		ignoreLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menu_operator(false);
			}
		});
		
		
		acceptLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menu_operator(true);
			}
		});
		
		
	}
	
	
	private void menu_operator(boolean bool){
		int []values = new int[]{};
		int count = friendList.size();
		for (int i = 0; i < count; i++) {
			Chat chat = friendList.get(i);
			if(chat.isOperate()){
				if(!bool)
					tpChat.tp_chat_add_deny(BuddyInfoUtil.getAlias(Globals.myInfo), chat.getSkyid(), tpChat.tp_chat_getfriendsgid());
				else
					tpChat.tp_chat_add_permit(BuddyInfoUtil.getAlias(Globals.myInfo), chat.getSkyid(), tpChat.tp_chat_getfriendsgid());
				values[values.length] = i;
			}
		}
		if(values.length > 0){
			for (int i = 0; i < values.length; i++) {
				friendList.remove(values[i]);
			}
			handler.sendEmptyMessage(SETADAPTER);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
